package com.hadluo.qq.server.mapper;

import com.hadluo.qq.server.entity.Friendship;
import org.apache.ibatis.annotations.*;
import java.util.List;

/**
 * 好友关系Mapper接口
 * @author hadluo
 */
@Mapper
public interface FriendshipMapper {
    /**
     * 根据ID查询好友关系
     */
    @Select("SELECT * FROM friendships WHERE id = #{id}")
    Friendship selectById(Long id);

    /**
     * 查询用户的所有好友关系
     */
    @Select("SELECT id, user_id, friend_id, status, remark, create_time, update_time FROM friendships WHERE user_id = #{userId}")
    List<Friendship> selectByUserId(Long userId);

    /**
     * 查询两个用户之间的好友关系
     */
    @Select("SELECT id, user_id, friend_id, status, remark, create_time, update_time FROM friendships WHERE user_id = #{userId} AND friend_id = #{friendId}")
    Friendship selectByUserIdAndFriendId(@Param("userId") Long userId, @Param("friendId") Long friendId);

    /**
     * 查询用户的好友申请
     */
    @Select("SELECT id, user_id, friend_id, status, remark, create_time, update_time FROM friendships WHERE friend_id = #{userId} AND status = 0")
    List<Friendship> selectFriendRequests(Long userId);

    /**
     * 插入好友关系
     */
    @Insert("INSERT INTO friendships (user_id, friend_id, remark, status, create_time, update_time) " +
            "VALUES (#{userId}, #{friendId}, #{remark}, #{status}, #{createTime}, #{updateTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(Friendship friendship);

    /**
     * 更新好友关系
     */
    @Update("UPDATE friendships SET remark = #{remark}, status = #{status}, update_time = #{updateTime} WHERE id = #{id}")
    int update(Friendship friendship);

    /**
     * 删除好友关系
     */
    @Delete("DELETE FROM friendships WHERE id = #{id}")
    int delete(Long id);

    /**
     * 批量删除好友关系
     */
    @Delete("DELETE FROM friendships WHERE (user_id = #{userId} AND friend_id = #{friendId}) OR (user_id = #{friendId} AND friend_id = #{userId})")
    int deleteByUserIdAndFriendId(@Param("userId") Long userId, @Param("friendId") Long friendId);
}